package com.gip.controller;

import com.alibaba.fastjson.JSONArray;
import com.gip.mapper.DataMapper;
import com.gip.pojo.Cost;
import com.gip.pojo.Customer;
import com.gip.pojo.Order;
import com.gip.pojo.User;
import com.gip.service.CustomerService;
import com.gip.service.OrderService;
import com.gip.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TableController {
    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;
    @Autowired
    CustomerService customerService;
    @Autowired
    DataMapper dataMapper;


    @RequestMapping("/table")
    public String table() {
        return "table";
    }

    @RequestMapping("/edit")
    public String edit(int id, Model model) {
        User user = userService.findUserById(id);
        model.addAttribute("user", user);
        return "table/edit";
    }

    @RequestMapping("/add")
    public String add() {
        return "table/add";
    }

    @RequestMapping("/orderEdit")
    public String orderEdit(int id, Model model) {
        Order order = orderService.findOrderById(id);
        model.addAttribute("order", order);
//        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String payDate = sd.format(order.getPayDate());
//        String MaturityDate = sd.format(order.getMaturityDate());
//        model.addAttribute("payDate", payDate);
//        model.addAttribute("MaturityDate", MaturityDate);
        System.out.println(order);
        return "table/orderEdit";
    }

    @RequestMapping("/orderAdd")
    public String orderAdd(Model model) {
        List<Cost> cost = dataMapper.findCost();
        for (Cost cost1 : cost) {
            model.addAttribute(cost1.getTypeName(),cost1.getValue());
        }
        return "table/orderAdd";
    }

    @RequestMapping("/customerEdit")
    public String customerEdit(int id, Model model) {
        Customer customer = customerService.findCustomerById(id);
        model.addAttribute("customer", customer);
        List<String> allMajor = dataMapper.findAllMajor();
        System.out.println(allMajor);
        model.addAttribute("allMajor", allMajor);
        return "table/customerEdit";
    }
    @RequestMapping("/customerAdd")
    public String customerAdd(Model model) {
        List<String> allMajor = dataMapper.findAllMajor();
        model.addAttribute("allMajor", allMajor);
        return "table/customerAdd";
    }

}
